package ooday04;

public class PolymorphicDmo {
    public static void main(String[] args) {
        //演示强转成功的条件
        Animal o=new Dog("小黑",2,"灰");//向上造型
        Dog g=(Dog) o;//引用o所指向的对象,就是Dog类型
        Swim s=(Swim) o;//引用o所指向的对象,实现了Swim接口
        //Fish f=(Fish) o;

        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof  Fish);//false
        if (o instanceof  Fish){//false
            Fish f=(Fish) o;
        }
//演示向上造型(多态)的第2点应用:
        //Animal o = new Animal(); //编译错误，抽象类不能被实例化
        Animal[] animals =new Animal[5];
        animals[0]=new Dog("小黑",2,"白");
        animals[1]=new Dog("小白",3,"黑");
        animals[2]=new Dog("小花",2,"白");
        animals[3]=new Dog("小绿",5,"灰");
        animals[4]=new Dog("小青",2,"绿");
        for(int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].drink();
            animals[i].eat();// 每个动物吃饭

            if (animals[i] instanceof  Dog ){//只能适用于Dog类
                Dog dog =(Dog) animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof  Chick){//只能适用于Chick类
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Fish){//适用于所有实现Swim接口的（会游泳的）
                Fish fish=(Fish) animals[i];
                fish.swim();
            }
        }



//        //演示向上造型(多态)的第一点应用
//        Master master=new Master();
//        Dog dog=new Dog("小花",4,"花");
//        Chick chick=new Chick("小白",4,"绿");
//        Fish fish =new Fish("小鱼",5,"红");
//        master.feed(dog); //在传参的同时,系统自动做了向上造型
//        master.feed(chick);
//        master.feed(fish);

//        //演示向上造型
//        Dog o1=new Dog("小黑",4,"灰");//狗是狗
//        //o1. name/age/color/drink()/eat()/swim/lookHome();
//        Animal o2=new Dog("小白",1,"黑");//狗是动物--向上造型
//        //o2.name/age/color/drink()/eat()
//        Swim o3=new Dog("小花",4,"花");//狗会游泳--向上造型
//        //o3.swim();


    }
}
